<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>回调页面</title>
    <script type="text/javascript" src="script/jquery-1.8.0.min.js"></script>
    <script src="plugins/axios/dist/axios.js"></script>
    <script src="plugins/vue/dist/vue.js"></script>
    <script src="js/common.js"></script>
</head>
<body>
<div id="callbackDiv">

</div>
<script type="text/javascript">
    // 回调页面的主要任务就是拿到用户在微信授权后，微信给出的code;
    //然后调用后端接口将code传给后端
    new Vue({
        el: "#callbackDiv",
        data:{},
        methods:{},
        mounted(){
            //加载完成页面就要来执行
            //1.获取授权完微信提供的code
            let urlObj=str2Obj(location.href);
            let codeObj={code:urlObj.code};
            //2.调用后端接口，通过code去微信获取accesstoken令牌和微信用户的openid
            this.$http.post("/logininfo/wechat/login",codeObj)
                .then(result =>{
                    result = result.data;
                    if (result.success){
                        //说明后端已经做了免密登录且登录成功
                        let {token,logininfo}=result.result;
                        localStorage.setItem("token",token);
                        localStorage.setItem("logininfo",JSON.stringify(logininfo));
                        location.href="/index.html";
                        alert("登录成功！");
                    }else if (!result.success&&result.message=="toBind"){
                        //说明该微信用户还没有绑定到平台网站，需要跳转到绑定页面进行绑定
                        let {accessToken,openid}=result.result;
                        //需要携带者accessToken和openid跳转到绑定页面
                        location.href="/binder.html?openid="+openid+"&accessToken="+accessToken;
                    }
                }).catch(result=>{
                    alert("系统错误！");
            })
        }
    })
</script>

</body>
</html>